angular.module('app').controller('homeCtrl', ['$scope', '$http', 'userInfo', 'utils', 'product', '$state', function($scope, $http, userInfo, utils, product, $state) {
	$scope.bannerList = [{
		imgurl: "image/banner_empty.png"
	}]
	$scope.isnewRegester=userInfo.isnewRegester
	/*初始化swiper*/
	var swiper = new Swiper('.swiper-container', {
		pagination: '.swiper-pagination',
		paginationClickable: true,
		autoplay: 3000,
		autoplayDisableOnInteraction: false,
		loop: true,
		observer: true,
		observeParents: true,
	});
	/*banner*/
	$http.get(utils.bannerSys, {
		params: {
			header: {
				gps: "",
				rep_seq: "sn" + new Date().getFullYear() + new Date().getMonth() + new Date().getDate() + new Date().getTime(),
				sign: "",
				tra_code: "050011"
			},
			body: {
				req_time: "2017-06-20"
			}
		}
	}).then(function(res) {
		if(res.header.re_code == "0000") {
			$scope.bannerList = res.body.imglist
		}
	})
	/*banner end*/

	$scope.tiaozhuan=function(go){
		userInfo.isnewRegester=false
        if(go==1){
			$state.go("user_coupon")
		}
	}
	

	/*四宫格*/
	$scope.block = [{
		areatitle: "--",
		arealink: "--"
	}, {
		areatitle: "--",
		arealink: "--"
	}, {
		areatitle: "--",
		arealink: "--"
	}, {
		areatitle: "--",
		arealink: "--"
	}]
	$http.get(utils.bannerSys, {
		params: {
			header: utils.header("050021"),
			body: {

			}
		}
	}).then(function(res) {
		if(res.header.re_code == "0000") {

			$scope.block = res.body.arealist

		}
	})

	/*获取是否新手*/
	userInfo.getUserIsNew()

	$scope.productList = []

	/*排序*/
	$http.post(utils.productSys, {}, {
		params: {
			header: utils.header("030050"),
			body: {
				userType: userInfo.is_new_user
			}
		}
	}).then(function(res) {
		$scope.xinshou = [] //新手数组
		$scope.cpOver = [] //已满标数组
		if(res.header.re_code == "0000") {
			$scope.productList = res.body.productInfoList
			for(var i = 0; i < $scope.productList.length; i++) {
				if($scope.productList[i].pro_share == "0") {
					$scope.cpOver.push($scope.productList[i])
					$scope.productList.splice(i, 1)
					i--
					continue
				} else {
					if($scope.productList[i].is_new_user_mark == 0) {
						$scope.xinshou.push($scope.productList[i])
						$scope.productList.splice(i, 1)
						i--
					}
				}
			}
			/*是否是新手*/
			if(!userInfo.user_code || userInfo.is_new_user == 0) {
				if($scope.xinshou.length != 0) {
					$scope.productList = $scope.xinshou
				}
			}
			/*已满标显示期限最长的*/
			if($scope.productList.length == 0) {
				if($scope.cpOver.length != 0) {
					$scope.cpOver.sort(function(a, b) {
						return b.term - a.term
					})
					$scope.productList = $scope.cpOver
				}

			}

		}
	})

	/*购买*/

	$scope.buy = function(item) {
		product.productInfo = item
		userInfo.buy(item)
	}

	$scope.gourl = function(item) {
		$state.go("productInfo", {
			productId: item.product_id
		})
		product.productInfo = item
	}

}])